﻿$(function () {
    var cardViewThreshold = 580;
    /* Set a locale manually */
    $.extend($.fn.bootstrapTable.defaults, $.fn.bootstrapTable.locales['en-US']);
    loaddata();
    
    /* Auto adjust the table's view type (table/card) when screen size changes */
    //window.onresize = function () {
    //    $('#table').bootstrapTable('refreshOptions', {
    //        cardView: document.body.clientWidth < cardViewThreshold
    //    });
    //};
});
function reloaddata() {
    //var opt = {
    //    url: "/online/listvacation/"
    //};
    //$('#table').bootstrapTable('refresh', opt);

    $.ajax({
        url: '/online/listvacation/',
        type: 'get',
        dataType: 'json',
        headers: { "X-CSRFToken": getCookie('csrftoken') },
        success: function (data) {
            if (data && data.total > 0) {
                $("#table").bootstrapTable('load', data.rows);
            } else {
                //alert('error');
            }
        }
    })  
}

function loaddata() {
    $.ajax({
        url: '/online/listvacation/',
        type: 'get',
        dataType: 'json',
        headers: { "X-CSRFToken": getCookie('csrftoken') },        
        success: function (data) {
            if (data && data.total>0) {
                binddata(data.rows);
                $('.columns-right').append('<button class="btn btn-primary" type="button" data-toggle="modal" data-target="#myModal" name="toggle" aria-label="toggle" title=""> 申请 </button>&nbsp;<button class="btn btn-primary" style="margin-left:4px;" type="button" onclick="vacationtj()" name="toggle" aria-label="toggle" title=""> 统计 </button>');
                $('.keep-open').hide();
                var uname = $('#usernamespan').html();
                $("#vac_person").empty().append("<option value='" + uname + "' mail=''>" + uname + "</option>").attr("disabled", "disabled");
                //getuserlist();//order last one load it.

                //alert('success');
            } else {
                //alert('error');
            }
        }
    })
}

function binddata(datas) {
    var cardViewThreshold = 580;
    $('#table').bootstrapTable({
        cardView: document.body.clientWidth < cardViewThreshold,
        striped: true,
        search: true,
        pagination: true,
        ageNumber: 1,
        pageSize: 10,
        pageList: [10, 20, 30, 50],
        showColumns: true,
        showToggle: false,
        showExport: false,
        columns: [{
            field: 'vac_person',
            title: '请假人',
            sortable: true
        }, 
        {
            field: 'vac_type',
            title: '请假类型',
            sortable: true
            },
        {
            field: 'vac_interval',
            title: '请假时长(天)',
            sortable: true
        },
        {
            field: 'vac_date',
            title: '开始时间',
            sortable: true
        },
        {
            field: 'vac_enddate',
            title: '结束时间',
            sortable: true
        },
        {
            field: 'vac_temp',
            title: '办公地点',
            sortable: true
        },
        {
            field: 'vac_reason',
            title: '请假原因',
            sortable: true
        }],
        data: datas
    });
}

function getuserlist() {
    $.ajax({
        url: '/online/getuserlist/',
        type: 'get',
        dataType: 'json',
        headers: { "X-CSRFToken": getCookie('csrftoken') },
        success: function (data) {
            if (data && data.length > 0) {
                $("#vac_person").empty();
                $("#vac_person2").empty();
                for (var i = 0; i < data.length; i++) {
                    $("#vac_person").append("<option value='" + data[i].USER + "' mail='" + data[i].MAIL + "'>" + data[i].USER + "</option>");
                    $("#vac_person2").append("<option value='" + data[i].USER + "' mail='" + data[i].MAIL + "'>" + data[i].USER + "</option>");
                }
                //alert('success');
            } else {
                //alert('error');
            }
        }
    })
}